package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataDailyPayBusinessTypeDTO;
import com.ctshk.rpc.system.entity.MainDataDailyPayBusinessType;
import com.ctshk.rpc.system.mapper.MainDataDailyPayBusinessTypeMapper;
import com.ctshk.rpc.system.req.MainDataDailyPayBusinessTypeIdReq;
import com.ctshk.rpc.system.req.MainDataDailyPayBusinessTypeListReq;
import com.ctshk.rpc.system.req.MainDataDailyPayBusinessTypeReq;
import com.ctshk.rpc.system.service.IMainDataDailyPayBusinessTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日結業務類型表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-16
 */
@DubboService
public class MainDataDailyPayBusinessTypeServiceImpl extends ServiceImpl<MainDataDailyPayBusinessTypeMapper, MainDataDailyPayBusinessType> implements IMainDataDailyPayBusinessTypeService {

    @Override
    public Result add(MainDataDailyPayBusinessTypeReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        long id = IdWorker.getId();
        MainDataDailyPayBusinessType businessType = EntityUtil.copy(req, MainDataDailyPayBusinessType.class);
        businessType.setId(id);
        businessType.setCreateId(tokenUser.getId());
        businessType.setModifiedId(tokenUser.getId());
        businessType.setModifiedName(tokenUser.getUsername());
        businessType.setGmtCreate(now);
        businessType.setGmtModified(now);
        businessType.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(businessType);
        return Result.success(id);
    }

    @Override
    public Result update(MainDataDailyPayBusinessTypeReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataDailyPayBusinessType businessType = EntityUtil.copy(req, MainDataDailyPayBusinessType.class);
        businessType.setModifiedId(tokenUser.getId());
        businessType.setModifiedName(tokenUser.getUsername());
        businessType.setGmtModified(now);
        baseMapper.updateById(businessType);
        return Result.success(req.getId());
    }

    @Override
    public MainDataDailyPayBusinessTypeDTO query(MainDataDailyPayBusinessTypeIdReq req) {
        MainDataDailyPayBusinessType mainDataDailyPayBusinessType = baseMapper.selectById(req.getId());
        MainDataDailyPayBusinessTypeDTO businessTypeDTO = EntityUtil.copy(mainDataDailyPayBusinessType, MainDataDailyPayBusinessTypeDTO.class);
        return businessTypeDTO;
    }

    @Override
    public PageResponse<MainDataDailyPayBusinessTypeDTO> list(MainDataDailyPayBusinessTypeListReq req) {
        Page<MainDataDailyPayBusinessType> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataDailyPayBusinessType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataDailyPayBusinessType::getStatus,req.getStatus());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()),MainDataDailyPayBusinessType::getDailyPayBusinessCode,req.getName()).or().like(StringUtils.isNotBlank(req.getName()),MainDataDailyPayBusinessType::getDailyPayBusinessType,req.getName());
        queryWrapper.lambda().orderByDesc(MainDataDailyPayBusinessType::getGmtModified);
        Page<MainDataDailyPayBusinessType> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataDailyPayBusinessType> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataDailyPayBusinessTypeDTO> mainDataDailyPayBusinessTypeDTOS = EntityUtil.copyList(records, MainDataDailyPayBusinessTypeDTO.class);
        return new PageResponse<>(mainDataDailyPayBusinessTypeDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
